package com.caowei.weicimpress.weicimpress;

import android.annotation.TargetApi;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Build;

/**
 * Created by caoweiPc on 2014/7/25.
 * 该异步任务与 TaskFragment 业务绑定
 */
public  abstract class MyAsyncTask<Params, Progress, Result> {
    private MyApplication application;
    private AsyncTask<Params, Progress, Result> asyncTask;
    private   void init(final MyApplication application, final String taskName){
        asyncTask=new AsyncTask<Params, Progress, Result>(){
            @Override
            protected Result doInBackground(Params... params) {
                Result result= MyAsyncTask.this.doInBackground(params);
                if(!isCancelled()){
                    application.removeAsyncTaskBean(this);
                    //todo 刷新任务表
                    application.sendBroadcast(new Intent(Utils.NOTIFYTASKCHANGE_ACTION));
                }
                return result;
            }

            @Override
            protected void onPostExecute(Result result) {
                super.onPostExecute(result);
                MyAsyncTask.this.onPostExecute(result);
            }

            @Override
            protected void onCancelled(final Result result) {
                AsyncTask<Object,Object,Boolean> asyncTask1=new AsyncTask<Object,Object,Boolean>() {
                    @Override
                    protected Boolean doInBackground(Object[] params) {
                        return MyAsyncTask.this.onCancelled(result);
                    }
                    @Override
                    protected void onPostExecute(Boolean b) {
                        if(b){
                            application.removeAsyncTaskBean(asyncTask);
                            application.sendBroadcast(new Intent(Utils.NOTIFYTASKCHANGE_ACTION));
                        }else{
                            String tip= application.getResources().getString(R.string.canceltaskerrortip,taskName);
                            throw new MyException(tip,MyException.ERROR);
                        }
                    }
                };

                if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB) {
                    asyncTask1.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
                }
                else {
                    asyncTask1.execute();
                }
            }
        };

        this.application=application;
        MyApplication.AsynTaskBean bean=new MyApplication.AsynTaskBean();
        bean.setAsyntaskName(taskName);
        bean.setTask(asyncTask);
        this.application.addAsyncTaskBean(bean);
        application.sendBroadcast(new Intent(Utils.NOTIFYTASKCHANGE_ACTION));
        //todo 考虑尝试用BroadcastReceiver 刷新任务表
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public final MyAsyncTask<Params, Progress, Result> execute(MyApplication application,String taskName
            ,Params... params) {
        init(application,taskName);
        if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB) {
            asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,params);
        }
        else {
            asyncTask.execute(params);
        }
        return this;
    }

    public final boolean isCancelled() {
        if(asyncTask!=null){
            return asyncTask.isCancelled();
        }
        return true;
    }
    protected  abstract Result doInBackground(Params... params);


    protected  abstract void onPostExecute(Result result);

    protected abstract boolean onCancelled(Result result);

}
